<?php

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class Download
{
    public static function downloadImg($url, $savaFilePath)
    {
        try {
            $client = new Client(['verify' => false]);
            $client->get($url, ['save_to' => $savaFilePath]);
            return file_exists($savaFilePath);
        } catch (\Exception $e) {
            return false;

        }
    }

    public static function downloadVedio($url, $savaPath)
    {

        $ts_content = file_get_contents($url);
        $ts_content = explode(',', $ts_content);

        $ts_file = array();
        foreach ($ts_content as $key => $value) {
            if ($key == 0) continue;
            $value = trim($value);
            $ts_file[] = substr($value, 0, strpos($value, '.ts') + 60);
        }

        $url_prefix = substr($url, 0, strpos($url, '.m3u8'));
        $url_prefix = substr($url, 0, strrpos($url, '/') + 1);
        $file_content = '';

        foreach ($ts_file as $key => $value) {
            $file_content .= file_get_contents($url_prefix . $value);
        }

        file_put_contents('tmp_out.ts', $file_content);
// FFMPEG_PATH 是你自己解压ffmpeg的bin路径，例如我的是F:/ffmpeg/bin/
        exec("ffmpeg -i tmp_out.ts {$savaPath}/tmp_out.mp4");
    }
}